package com.lx.algorithm.code.xly3.class04;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.TreeMap;

/**
 * Description:
 * Copyright:   Copyright (c)2019
 * Company:     zefu
 *
 * @author: 张李鑫
 * @version: 1.0
 * Create at:   2022-01-05 01:06:31
 * <p>
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2022-01-05     张李鑫                     1.0         1.0 Version
 */
public class Code01 {
    /**
     * 给你一个字符串类型的数组arr，譬如:
     * String[] arr = { "b\st", "d\", "a\d\e", "a\b\c" };
     * 把这些路径中蕴含的目录结构给打印出来，子目录直接列在父目录下面，并比父目录向右进两格，就像这样:
     * a
     * b
     * c
     * d
     * e
     * b
     * cst
     * d
     * 同一级的需要按字母顺序排列不能乱。
     *
     * todo:print 懒得写了
     * <p>
     * //Code01_GetFolderTree
     */
    public static class Tree {
        public String val;
        public TreeMap<String, Tree> ways;

        public Tree(String val, TreeMap<String, Tree> ways) {
            this.val = val;
            this.ways = ways;
        }

        public Tree(String p) {
            this.val = p;
            this.ways = new TreeMap<>();
        }

        @Override
        public String toString() {
            return "Tree{" +
                    "val='" + val + '\'' +
                    ", ways=" + ways +
                    '}';
        }
    }

    public static void getFolderTree(String[] str) {
        if (str == null || str.length == 0) {
            return;
        }

        //构建树
        Tree tree = new Tree("");
        for (int i = 0; i < str.length; i++) {
            String cur = str[i];
            String[] split = cur.split("\\\\");
            builder(tree.ways, split);
        }
        //打印
        System.out.println(JSONObject.toJSON(tree));
//        printProcess(tree);
    }

    public static void printProcess(Tree node, int level) {

    }


    public static void builder(TreeMap<String, Tree> tree, String[] strings) {
        TreeMap<String, Tree> cur = tree;
        for (int i = 0; i < strings.length; i++) {
            if (!cur.containsKey(strings[i])) {
                Tree t = new Tree(strings[i]);
                cur.put(strings[i], t);
            }
                cur = cur.get(strings[i]).ways;

        }
    }

    public static void main(String[] args) {
        String[] arr = { "b\\st", "d\\", "a\\d\\e", "a\\b\\c" };
        Node node = generateFolderTree(arr);
        getFolderTree(arr);
//        System.out.println(node);
//        System.out.println(JSONObject.toJSON(node));

    }

    public static class Node {
        // 上一个节点是通过哪条路，到我的
        public String path;
        // key : node下级的路   value：node在key这条路上对应的节点是什么
        public TreeMap<String, Node> nextMap;

        public Node(String p) {
            this.path = p;
            nextMap = new TreeMap<>();
        }

        @Override
        public String toString() {
            return "Node{" +
                    "path='" + path + '\'' +
                    ", nextMap=" + nextMap +
                    '}';
        }
    }

    // folderPaths ->  [   "a\b\c","a\b\s" , "a\d\e" ,"e\f\sty"     ]
    public static void print(String[] folderPaths) {
        if (folderPaths == null || folderPaths.length == 0) {
            return;
        }
        // 根据所有字符串，把前缀树建立好，头节点为head
        Node head = generateFolderTree(folderPaths);

        // 打印
        printProcess(head, 0);
    }

    public static Node generateFolderTree(String[] folderPaths) {
        Node head = new Node(""); // 系统根目录, 前缀树头节点
        for (String foldPath : folderPaths) { // 拿出每一个绝对路径
            String[] paths = foldPath.split("\\\\"); // java 特性，用一个"\"做分割的意思
            Node cur = head;
            for (int i = 0; i < paths.length; i++) { // "a"  , "b"   ,"c"
                if (!cur.nextMap.containsKey(paths[i])) {
                    cur.nextMap.put(paths[i], new Node(paths[i]));
                }
                cur = cur.nextMap.get(paths[i]);
            }
        }
        return head;
    }

    // head节点，当前在level层
    public static void printProcess(Node node, int level) {
        if (level != 0) {
            // 2 * (level - 1)
            System.out.println(get4nSpace(level) + node.path);
        }
        for (Node next : node.nextMap.values()) {
            printProcess(next, level + 1);
        }
    }

    public static String get4nSpace(int n) {
        String res = "";
        for (int i = 1; i < n; i++) {
            res += "    ";
        }
        return res;
    }


}